package Map
{
	import DataStore.BmpDataPool;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import GameLogic.ENUM;
	import GameLogic.Game;
	import LoadUnit.LoadManager;
	public class MapManager
	{
		public static var Width:int;
		public static var Height:int;
		public static var BgMap:Sprite;
		public static var TileMap:Sprite;
		public static var TileSize:int;
		
		private static var incrementId:int = 100;
		public function MapManager()
		{
		}
		
		public static function Init():void
		{
			Width = Game.Width;
			Height = Game.Height;
			TileSize = 50;
			BgMap = new Sprite();
			Game.MainCanvas.addChild(BgMap);
			TileMap = new Sprite();
			Game.MainCanvas.addChild(TileMap);
			
			LoadManager.AddLoadTask("Resource/Images/Map/map1.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/map2.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-1.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-2.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-3.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-4.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-5.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-6.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-7.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-8.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-9.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-10.png", ENUM.LOADIMGTYPE, ParseMap);
			LoadManager.AddLoadTask("Resource/Images/Map/image-11.png", ENUM.LOADIMGTYPE, ParseMap);
		}
		
		
		public static function PutTile(_type:int, _x:int, _y:int):void
		{
			var tile:Bitmap = new Bitmap(BmpDataPool.MapPool[_type]);
			tile.x = _x * TileSize;
			tile.y = _y * TileSize;
			TileMap.addChild(tile);
		}
		
		private static function ParseMap(_bmp:BitmapData):void
		{
			var w:int = _bmp.width / TileSize;
			var h:int = _bmp.height / TileSize;
			for (var i:int = 0; i < w; i++)
			{
				for (var j:int = 0; j < h; j++)
				{
					var tile:BitmapData = new BitmapData(TileSize, TileSize);
					tile.copyPixels(_bmp, new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize), new Point(0, 0));
                    BmpDataPool.AddMapTile(incrementId, tile);
					incrementId++;
				}
			}
		}
		
		public static function Temp():int
		{
			//trace("files:", loadTaskQueue.length);
			return 333;
		}
		
	}
}